﻿<template>
	<div class="mrsBill-container">
		<el-dialog v-model="state.isShowDialog" draggable width="769px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit />
					</el-icon>
					<span> {{ props.title }} </span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" size="default" label-width="80px" :rules="rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="产品包ID" prop="packageId">
							<el-input v-model="state.ruleForm.packageId" placeholder="请输入产品包ID" class="w100" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="包名称" prop="packageName">
							<el-input v-model="state.ruleForm.packageName" placeholder="请输入产品包名称" class="w100" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="流水号" prop="outBillId">
							<el-input v-model="state.ruleForm.outBillId" placeholder="请输入支付方交易流水号" class="w100" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="流水时间" prop="billTime">
							<!-- <el-input v-model="state.ruleForm.billTime" placeholder="请输入支付方交易流水时间" class="w100" clearable /> -->
							<el-date-picker v-model.trim="state.ruleForm.billTime" type="datetime" placeholder="选择日期"
								date-format="YYYY/MM/DD" time-format="hh:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
								style="width: 100%" :default-time="defaultDate" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="展示价格" prop="productPrice">
							<el-input-number v-model="state.ruleForm.productPrice" placeholder="请输入展示价格" class="w100"
								clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="红包" prop="luckPackage">
							<el-input-number v-model="state.ruleForm.luckPackage" placeholder="请输入红包" class="w100"
								clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="优惠金额" prop="preferentialPrice">
							<el-input-number v-model="state.ruleForm.preferentialPrice" placeholder="请输入优惠金额" class="w100"
								clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="交易价格" prop="productCost">
							<el-input-number v-model="state.ruleForm.productCost" placeholder="请输入交易价格" class="w100"
								clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="订单状态" prop="orderStatus">
							<el-select v-model.trim="state.ruleForm.orderStatus" placeholder="请选择" class="w100">
								<el-option label="待付款" :value="0"></el-option>
								<el-option label="已付款" :value="1"></el-option>
								<el-option label="已完成" :value="2"></el-option>
								<el-option label="已取消" :value="3"></el-option>
								<el-option label="待退款" :value="4"></el-option>
								<el-option label="已退款" :value="5"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="支付类型" prop="payType">
							<el-select v-model.trim="state.ruleForm.payType" placeholder="请选择" class="w100">
								<el-option label="中国移动" :value="1"></el-option>
								<el-option label="中国联通" :value="2"></el-option>
								<el-option label="中国电信" :value="3"></el-option>
								<el-option label="微信支付" :value="4"></el-option>
								<el-option label="支付宝支付" :value="5"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="应答数据" prop="resData">
							<el-input v-model="state.ruleForm.resData" placeholder="请输入应答数据" class="w100" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="请求地址" prop="requestUrl">
							<el-input v-model="state.ruleForm.requestUrl" placeholder="请输入请求地址" class="w100" clearable
								type="textarea" :autosize="{ minRows: 2 }" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="state.ruleForm.remark" placeholder="请输入备注" class="w100" clearable
								type="textarea" :autosize="{ minRows: 2 }" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel" size="default">取 消</el-button>
					<el-button type="primary" @click="submit" size="default">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="editMrsBill" >
import { reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import type { FormRules } from "element-plus";
import mittBus from '/@/utils/mitt';
import { getNowTime } from '/@/utils/formatTime';
import { MrsBill } from '/@/api/models';
import SysMrsBillApi from '/@/api/memberRights/bill';

// 定义变量内容
const props = defineProps({
	title: String,
});
const ruleFormRef = ref();
const state = reactive({
	isShowDialog: false,
	ruleForm: {} as MrsBill,
});

// 刚进入页面给的默认当前日期
const defaultDate = getNowTime();

// 删除非必填规则
const rules = ref<FormRules>({
	packageId: [{ required: true, message: '请输入产品包ID！', trigger: 'blur', },],
	packageName: [{ required: true, message: '请输入产品包名称！', trigger: 'blur', },],
	billTime: [{ required: true, message: '请输入支付方交易流水时间！', trigger: 'blur', },],
});

// 打开弹窗
const openDialog = (row: MrsBill) => {
	state.ruleForm = row;
	state.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	mittBus.emit('submitRefresh');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit = () => {
	ruleFormRef.value.validate(async (valid: boolean, fields?: any) => {
		if (valid) {
			let res;
			if (state.ruleForm.id != undefined && state.ruleForm.id > 0)
				res = await SysMrsBillApi().editPost(state.ruleForm);
			else
				res = await SysMrsBillApi().addPost(state.ruleForm);
			if (res.data.resCode != '0000')
				ElMessage.error(res.data.resMessage);
			else {
				ElMessage.success("操作成功");
				closeDialog();
			}
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

// 导出对象 将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>